﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace PMCT
{
    [Serializable]
    [XmlRoot(ElementName = "ConfigRoot", IsNullable = false, Namespace = "")]
    public class ConfigRoot : ICloneable
    {
        public ConfigRoot() { }
        public ConfigRoot(string v, string n) { version = v; name = n; }

        [XmlAttribute]
        public string version;

        [XmlAttribute]
        public string name; //当前配置名称，如：PMBOK配置

        [XmlArray]
        public List<Dimension> listDimension = new List<Dimension>(); //维度列表

        [XmlArray]
        public List<Item> listItem = new List<Item>();

        [XmlArray]
        public List<Unit> listUnit = new List<Unit>();


        #region ICloneable Members

        public object Clone()
        {
            using (Stream objectStream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objectStream, this);
                objectStream.Seek(0, SeekOrigin.Begin);
                return (ConfigRoot)formatter.Deserialize(objectStream);
            }
        }

        #endregion
    }

    [Serializable]
    public class DimensionItem
    {
        public DimensionItem() { }
        public DimensionItem(string n) { guid = Guid.NewGuid(); name = n; }

        [XmlAttribute]
        public Guid guid; //唯一标识

        public string name; //维度，如：整体管理，范围管理
    }
    [Serializable]
    public class Dimension
    {
        public string name; //维度名称，如：知识领域，过程组，干系人，项目等

        [XmlAttribute]
        public bool isMultiple;

        [XmlArray]
        public List<DimensionItem> list = new List<DimensionItem>();
    }

    // 项，即过程
    [Serializable]
    public class Item : ICloneable
    {
        public Item() { }
        public Item(string n) { guid = Guid.NewGuid(); name = n; }

        [XmlAttribute]
        public Guid guid;

        public string name;

        [XmlArray]
        public List<Guid> dimension = new List<Guid>();

        [XmlArray]
        public List<Guid> input = new List<Guid>();

        [XmlArray]
        public List<Guid> output = new List<Guid>();

        [XmlArray]
        public List<Guid> tools = new List<Guid>();

        #region ICloneable Members

        public object Clone()
        {
            using (Stream objectStream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objectStream, this);
                objectStream.Seek(0, SeekOrigin.Begin);
                return (Item)formatter.Deserialize(objectStream);
            }
        }

        #endregion
    }

    // 单元，即输入，输出，工具
    [Serializable]
    public class Unit
    {
        public Unit(string n, string u) { guid = Guid.NewGuid(); name = n; uri = u; isTool = false; }
        public Unit() { guid = Guid.NewGuid(); }

        [XmlAttribute]
        public Guid guid;

        public string name;
        public string uri;
        public bool isTool;
    }
}
